package com.cino.system.controller;


import com.cino.basic.util.AjaxResult;
import com.cino.basic.util.PageList;
import com.cino.system.domain.DictionaryDetail;
import com.cino.system.query.DictionaryDetailQuery;
import com.cino.system.service.DictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/dictionaryDetail")
public class DictionaryDetailController {

    @Autowired
    private DictionaryDetailService dictionaryDetailService;

    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public DictionaryDetail loadById(@PathVariable("id") Long id){
        return dictionaryDetailService.loadById(id);
    }

    @ApiOperation(value = "查询所有" )
    @GetMapping
    public List<DictionaryDetail> loadAll(){
        return dictionaryDetailService.loadAll();
    }

    @ApiOperation(value = "通过ID删除" )
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            dictionaryDetailService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @ApiOperation(value = "添加或修改" )
    @PutMapping
    public AjaxResult addorUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if(dictionaryDetail.getId()==null){
                dictionaryDetailService.save(dictionaryDetail);
            }else{
                dictionaryDetailService.update(dictionaryDetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @ApiOperation(value = "分页查询" )//高级查询和分页查询都用的post
    @PostMapping
    public PageList<DictionaryDetail> pageQuery(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
        return dictionaryDetailService.pageQuery(dictionaryDetailQuery);
    }

    @ApiOperation(value ="接口：批量删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            //1.调用service的方法
            dictionaryDetailService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

}
